#include <stdio.h>
#include <math.h>
#include <SDL2/SDL.h>
#include "Ball.h"
// #include "Resource.h"
#include "Config.h"
#include "RectShape.h"


#define RECTSHAPE_COUNT 3

SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;

RectShape *first;
RectShape *last;


void event_loop();
void draw();
void addRectShape(RectShape *rectShape);


int main(int args, char *argv[])
{
    if (SDL_Init(SDL_INIT_VIDEO)) {
        SDL_Log("Can not init video, %s", SDL_GetError);
        return 1;
    }

    window = SDL_CreateWindow("SDL2 Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
    if (window == NULL)  {
        SDL_Log("Can not create Window, %s", SDL_GetError());
        return 1;
    }

    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    if (renderer == NULL) {
        SDL_Log("Can not Create Renderer, %s", SDL_GetError());
    }
    
    first = NULL;
    last = NULL;
    addRectShape(RectShape_Create(0,    100, 80, 60, 0xffff0000));
    addRectShape(RectShape_Create(100,  100, 80, 60, 0xff00ff00));
    addRectShape(RectShape_Create(200,  100, 80, 60, 0xff0000ff));

    event_loop();

    RectShape *item = first;
    while (item != NULL) {
        RectShape *current = item;
        item = item->next;
        RectShape_Destroy(current);
    }
    first = NULL;
    last = NULL;

    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();
    
    return 0;
}

void event_loop()
{
    RectShape *item;
    while (1) {
        uint32_t begin = SDL_GetTicks();
        draw();
        SDL_Event event;
        while (SDL_PollEvent(&event)) {
            switch (event.type)
            {
                case SDL_MOUSEMOTION:
                case SDL_MOUSEBUTTONUP:
                case SDL_MOUSEBUTTONDOWN:
                    item = last;
                    while (item != NULL) {
                        if (RectShape_OnMouseEvent(item, &event)) {
                            if (item != last) {
                                if (item->pre != NULL) {
                                    item->pre->next = item->next;
                                } else {
                                    first = item->next;
                                }
                                item->next->pre = item->pre;
                                item->next = NULL;
                                addRectShape(item);
                            }
                            break;
                        }
                        item = item->pre;
                    }
                    // SDL_Log("Mouse move(x:%d y:%d)", event.motion.x, event.motion.y);  
                    break;
                // case SDL_MOUSEBUTTONDOWN:
                //     SDL_Log("Mouse Down(x:%d y:%d)", event.button.x, event.button.y);  break;
                // case SDL_MOUSEBUTTONUP:
                //     SDL_Log("Mouse Up  (x:%d y:%d)", event.button.x, event.button.y);  break;
                case SDL_QUIT: return;
            }
        }
        long current = SDL_GetTicks();
        long cost = current - begin;
        long  frame = 1000 / FRAMERATE;
        long delay = frame - cost;

        if (delay > 0) {
            SDL_Delay(delay);
        }
    }
}

void addRectShape(RectShape *rectShape)
{
    if (last != NULL) {
        last->next = rectShape;
        rectShape->pre = last;
        last = rectShape;
    } else {
        last = rectShape;
        first = rectShape;
    }
}

void draw()
{
    SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
    SDL_RenderClear(renderer);

    RectShape *item = first;
    while (item != NULL) {
        RectShape_Draw(item, renderer);
        item = item->next;
    }

    SDL_RenderPresent(renderer);
}
